/*
 * Copyright (c) 2012-2013, CS Bakery. All rights reserved.
 */

package csBakery;

import java.util.*;
import java.lang.String;

/*
 * 	@release Final Release
 *	@author  Lee Cheuk Wang
 *	@version 4.0.2
 *
 * 	@release Release 2
 *	@author  Lee Cheuk Wang
 *	@version 3.0.9
 *
 * 	@release Release 1
 *	@author  Chan Ka Yan
 *	@version 1.0.2
 */
 
public class IO {
	static Scanner scanner;
	final static int OPTIONNO = 9;
	StockController stockController;
	static LogController logController;
	static BalanceController balanceController;
	public IO(){
		stockController = new StockController();
		logController = new LogController(LogController.defaultLink);
		stockController.loadStockRecord();
		balanceController = new BalanceController(PriceList.defaultLink, CostList.defaultLink, LogController.defaultLink);
	}
	public static int inputFromUser(String str, boolean isOption) throws Exception{
		int num = Integer.parseInt(str);
		if (!isOption && num <= -1){
			throw new Exception("Qty Error");
		}
		if (isOption && (num > IO.OPTIONNO || num <= 0)){
			throw new Exception("Option Error");
		}
		return num;
	}
	
	private void BreadQtyUpdate(boolean isSale){
		Vector<String> vector = stockController.getStockProductList();
		int num = 0;
		String input;
		boolean flag = true;
		for (int i = 0; i < vector.size(); i++){
			do{
				if (!isSale){
					System.out.print("Please enter the number of " + vector.elementAt(i) + " imported: ");
				}else{ 
					System.out.print("Please enter the number of " + vector.elementAt(i) + " bread saled: ");
				}
				
				try{
					input = scanner.nextLine();
					num = inputFromUser(input, false);
					if (!isSale){
						if (num != 0)
							stockController.updateStock(logController, vector.elementAt(i), num); //update new stock
						flag = false;
					}
					else{
						if (num != 0){
							flag = !(stockController.updateSale(logController, vector.elementAt(i), num));
						}
						else{
							flag = false;
						}
						if (flag){
							System.out.println("Error. The number of sales is greater than the number of stock!");
						}
					}
				} catch (Exception e){
					System.out.println("Invalid Input!");
				}
			}while (flag);
		}
	}

	public boolean executeCommand(int option){
		switch (option){
		case 1:
			BreadQtyUpdate(false);
		break;
		case 2:
			BreadQtyUpdate(true);
		break;
		case 3:
			stockController.stockState();
			System.out.println("Log:");
			logController.printLog();
		break;
		case 4:
			stockController.loadStockRecord();
		break;
		case 5:
			stockController.saveStockRecord();
		break;
		case 6:
			showPriceCost();
		break;
		case 7:
			updateCurrent();
			System.out.println(balanceController.generateDayBalance());
		break;
		case 8:
			updateCurrent();
			System.out.println(balanceController.generateMonthBalance());
		break;
		case 9:
			logController.saveLog();
			return true;
		}
		return false;
	}
	private void updateCurrent(){
		logController.saveLog();
		balanceController = new BalanceController(PriceList.defaultLink, CostList.defaultLink, LogController.defaultLink);
	}
	
	private void showPriceCost(){
		PriceList priceList = new PriceList(PriceList.defaultLink);
		CostList costList = new CostList(CostList.defaultLink);
		System.out.println("Price List");
		System.out.println("==========");
		System.out.print(priceList);
		System.out.println("\nCost List");
		System.out.println("==========");
		System.out.print(costList);
		System.out.print("\n");
	}

	public void setScanner(Scanner scan){
		IO.scanner = scan;
	}
	
	public static void printOptions(){
		System.out.println("Welcome to CS Bakery System, Please indicate your choices:");
		System.out.println("1. Update bread stock (imported)");
		System.out.println("2. Update bread stock (saled)");
		System.out.println("3. View stock information");
		System.out.println("4. Import stock from existing record");
		System.out.println("5. Export stock record to file");
		System.out.println("6. Show price and cost");
		System.out.println("7. Generate day balance sheet");
		System.out.println("8. Generate month balance sheet");
		System.out.println("9. Exit the System");
	}
	
	public static void main(String[] args){
		IO io = new IO();
		scanner = new Scanner(System.in);
		processInput(io);
	}
	
	public static void processInput(IO io){
		int option = 0;
		String input;
		boolean isEnd = false;
		do{
			printOptions();
			try{
				input = scanner.nextLine();
				option = inputFromUser(input, true);
			} catch (Exception e){
				System.out.println("Option Error");
				continue;
			}
			isEnd = io.executeCommand(option);
		}while (!isEnd);
	}
}